
********************************************************************************	
cd $pathdata_processed
use terminales_fin, clear

sum chance
replace chance = chance/`r(sd)'
replace expected_gpa = expected_gpa/`r(sd)'
replace predicted_gpa = predicted_gpa/`r(sd)'

rename stdteachers_grades teach
rename stdteachers_grades2 teach2
********************************************************************************	    
global controls teach teach2 stdms_gpa stdms_gpa2 m_ms_gpa ///
		 female ///
		 age age2 ///
		 p_age p_age2 m_mage m_fage ///
		 p_educ p_educ2 m_mbu_kl m_fbu_kl ///
		 ln_p_inc m_mwyrkinnt_c m_fwyrkinnt_c 			
************************************************************************
global partial_gpa i.year_vg3 i.skole_fo

gen all = 1

egen teachq = xtile(teach), nq(2) 
gen lability = inlist(teachq, 1)
gen hability = inlist(teachq, 2)
		
gen f = female
gen m = 1 - female
********************************************************************************
cd $pathtable	
*******************************************************************************	
label var chance "$widetilde{\textrm{GPA_luck}}$"
label var predicted_gpa "$\textrm{GPA_luck}$"

label var stdexam_grades "Exam grades in 3\textsuperscript{rd} year"
label var stdvide3 "High school GPA"
label var completion "On time HS diploma"
label var hs_gs_c "Ever HS diploma"
label var he_e "Ever higher education"
label var share_available "Share of available HE programs"
label var rank_f_he "Selectivity of HE enrollment"
label var bu_sup "Number of completed years in HE"
label var first_job "Ever employed"
label var ln_wyrkinnt_c1 "First job labor income"
label var job10 "Employed 8 years after the exams"
label var ln_wyrkinnt_c10 "Labor income 8 years after the exams"
*******************************************************************************			
//By ability level
rename hability h
rename lability l
foreach sample in h l {
	foreach var in stdvide3 job10 ln_wyrkinnt_c10 {		
		
				eststo `sample'`var': ivregress 2sls `var' (predicted_gpa = chance) $controls $partial_gpa if `sample' == 1, cluster(group) rob 
				su `var' if e(sample) 
				estadd r(mean): `sample'`var'
				}
			}
		
		esttab lstdvide3 ljob10 lln_wyrkinnt_c10 hstdvide3 hjob10 hln_wyrkinnt_c10 using "mainRF_heterogeneity.tex", replace booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("& High school & Employed 8 & Annual labor & High school & Employed 8 & Annual labor \\" "& GPA & years after & income 8 years & GPA & years after & income 8 years \\" "& & the exams & after the exams & & the exams & after the exams \\" "& & & (log) & & & (log) \\" "\midrule" "\textit{Panel A:} & \multicolumn{3}{c}{\textit{Low Ability, Below Median Course Grades}} & \multicolumn{3}{c}{\textit{High Ability, Above Median Course Grades}} \\" "\cmidrule(lr){2-4} \cmidrule(lr){5-7} \\") /// 
		fragment stats(mean N, label("Mean dep. var." "N") fmt(%9.3f 0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
							
		eststo clear
********************************************************************************	
//By gender
drop  h l
rename m l
rename f h
foreach sample in h l {
	foreach var in stdvide3 job10 ln_wyrkinnt_c10 {		
		
				eststo `sample'`var': ivregress 2sls `var' (predicted_gpa = chance) $controls $partial_gpa if `sample' == 1, cluster(group) rob 
				su `var' if e(sample) 
				estadd r(mean): `sample'`var'
				}
			}
		
		esttab lstdvide3 ljob10 lln_wyrkinnt_c10 hstdvide3 hjob10 hln_wyrkinnt_c10 using "mainRF_heterogeneity.tex", append booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("\midrule" "\textit{Panel B:} & \multicolumn{3}{c}{\textit{Boys}} & \multicolumn{3}{c}{\textit{Girls}} \\" "\cmidrule(lr){2-4} \cmidrule(lr){5-7} \\") /// 
		fragment stats(mean N, label("Mean dep. var." "N") fmt(%9.3f 0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
							
		eststo clear			
********************************************************************************	
//By parental income
drop  h l
egen p_incq = xtile(ln_p_inc), nq(2)
gen h = p_incq == 2
gen l = 1 - h
foreach sample in h l {
	foreach var in stdvide3 job10 ln_wyrkinnt_c10 {		
		
				eststo `sample'`var': ivregress 2sls `var' (predicted_gpa = chance) $controls $partial_gpa if `sample' == 1, cluster(group) rob 
				su `var' if e(sample) 
				estadd r(mean): `sample'`var'
				}
			}
		
		esttab lstdvide3 ljob10 lln_wyrkinnt_c10 hstdvide3 hjob10 hln_wyrkinnt_c10 using "mainRF_heterogeneity.tex", append booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("\midrule" "\textit{Panel C:} & \multicolumn{3}{c}{\textit{Below Median Parental Income}} & \multicolumn{3}{c}{\textit{Above Median Parental Income}} \\" "\cmidrule(lr){2-4} \cmidrule(lr){5-7} \\") /// 
		fragment stats(mean N, label("Mean dep. var." "N") fmt(%9.3f 0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
							
		eststo clear		
********************************************************************************	
//By parental education
drop  h l
egen p_educq = xtile(p_educ), nq(2)
gen h = p_educq == 2
gen l = 1 - h
foreach sample in h l {
	foreach var in stdvide3 job10 ln_wyrkinnt_c10 {		
		
				eststo `sample'`var': ivregress 2sls `var' (predicted_gpa = chance) $controls $partial_gpa if `sample' == 1, cluster(group) rob 
				su `var' if e(sample) 
				estadd r(mean): `sample'`var'
				}
			}
		
		esttab lstdvide3 ljob10 lln_wyrkinnt_c10 hstdvide3 hjob10 hln_wyrkinnt_c10 using "mainRF_heterogeneity.tex", append booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("\midrule" "\textit{Panel D:} & \multicolumn{3}{c}{\textit{Below Median Parental Education}} & \multicolumn{3}{c}{\textit{Above Median Parental Education}} \\" "\cmidrule(lr){2-4} \cmidrule(lr){5-7} \\") /// 
		fragment stats(mean N, label("Mean dep. var." "N") fmt(%9.3f 0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
							
		eststo clear	
********************************************************************************
//By length of university attainments
drop  h l
gen l0 = he_e == 0
gen h = bu_sup > 3 & !missing(bu_sup)
gen l = he_e == 1 & bu_sup <= 3

ivregress 2sls h (predicted_gpa = chance) $controls $partial_gpa if all == 1, cluster(group) rob 
ivregress 2sls l (predicted_gpa = chance) $controls $partial_gpa if all == 1, cluster(group) rob 

foreach sample in h l l0 {
	foreach var in stdvide3 job10 ln_wyrkinnt_c10 {		
		
				eststo `sample'`var': ivregress 2sls `var' (predicted_gpa = chance) $controls $partial_gpa if `sample' == 1, cluster(group) rob 
				su `var' if e(sample) 
				estadd r(mean): `sample'`var'
				}
			}

		esttab l0stdvide3 l0job10 l0ln_wyrkinnt_c10 using "mainRF_bu_sup.tex", replace booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("& High school GPA & Employed 8 years & Annual labor income \\" "& & after the exams & 8 years after the exams \\" "& & & (log) \\" "\midrule" "\multicolumn{4}{l}{\textit{Subsample with no higher education}} \\") ///
		fragment stats(mean N, label("Mean dep. var." "N") fmt(%9.3f 0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none)
		
		esttab lstdvide3 ljob10 lln_wyrkinnt_c10 using "mainRF_bu_sup.tex", append booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("\midrule" "\multicolumn{4}{l}{\textit{Subsample with a maximum of 3 years of completed higher education}} \\") ///
		fragment stats(mean N, label("Mean dep. var." "N") fmt(%9.3f 0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
		
		esttab hstdvide3 hjob10 hln_wyrkinnt_c10 using "mainRF_bu_sup.tex", append booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("\midrule" "\multicolumn{4}{l}{\textit{Subsample with more than 3 years of completed higher education}} \\") ///
		fragment stats(mean N, label("Mean dep. var." "N") fmt(%9.3f 0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
		
		eststo clear	
********************************************************************************
//Detailed heterogeneity by ability
egen teachers_gradesq = xtile(teach), nq(5) 		
foreach i of numlist 1/5 {
	foreach var in stdvide3 job10 ln_wyrkinnt_c10 {	
	eststo `var'q`i': ivregress 2sls `var' (predicted_gpa = chance) $controls $partial_gpa if teachers_gradesq == `i', cluster(group) rob 
	su `var' if e(sample) 
	estadd r(mean): `sample'`var'q`i'
}	
}

		esttab stdvide3q1 stdvide3q2 stdvide3q3 stdvide3q4 stdvide3q5 using "het_GPA.tex", replace booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("& Bottom quintile of & 2\textsuperscript{nd} quintile of & 3\textsuperscript{rd} quintile of & 4\textsuperscript{th} quintile of & Top quintile of \\" "& HS course grade & HS course grade & HS course grade & HS course grade & HS course grade \\" "\midrule" "& \multicolumn{5}{c}{High school GPA} \\" "\midrule") ///
		fragment stats(mean N, label("Mean dep. var." "N") fmt(%9.3f 0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none)
		
		esttab job10q1 job10q2 job10q3 job10q4 job10q5 using "het_GPA.tex", append booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("\midrule" "& \multicolumn{5}{c}{Employed 8 years after the exams} \\" "\midrule") ///
		fragment stats(mean N, label("Mean dep. var." "N") fmt(%9.3f 0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none)
		
		esttab ln_wyrkinnt_c10q1 ln_wyrkinnt_c10q2 ln_wyrkinnt_c10q3 ln_wyrkinnt_c10q4 ln_wyrkinnt_c10q5 using "het_GPA.tex", append booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("\midrule" "& \multicolumn{5}{c}{Annual labor income 8 years after the exams (log)} \\" "\midrule") ///
		fragment stats(mean N, label("Mean dep. var." "N") fmt(%9.3f 0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none)		
		
		eststo clear			
